package com.hengfoxwolf.commonbase.base
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.ViewDataBinding
import com.hengfoxwolf.commonbase.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope

abstract class BaseActivity3<T : ViewDataBinding>(block: (LayoutInflater) -> T) : AppCompatActivity(), CoroutineScope by MainScope() {

    private val mBlock = block

    protected lateinit var mBinding : T

    //private lateinit var mRootViewBinding: ViewDataBinding
    //private lateinit var mRootBinding: Activity3BaseLayoutBinding
    //private lateinit var mRootBindingImpl: Activity3BaseLayoutBindingImpl

    private lateinit var mRootLayout: ConstraintLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //mRootBinding = DataBindingUtil.setContentView(this, R.layout.activity3_base_layout)
        //mRootBinding = DataBindingUtil.inflate(LayoutInflater.from(this@BaseActivity3),R.layout.activity3_base_layout,null,false)
        setContentView(R.layout.activity3_base_layout)
        mRootLayout = findViewById(R.id.cl_root)

        mBinding = mBlock.invoke(layoutInflater)
        val lpWidth = ConstraintLayout.LayoutParams.MATCH_PARENT
        val lpHeight = ConstraintLayout.LayoutParams.MATCH_PARENT

        mRootLayout.addView(
            mBinding.root,
            ConstraintLayout.LayoutParams(lpWidth, lpHeight)
        )

        init(savedInstanceState)
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.unbind()
    }

    abstract fun init(savedInstanceState: Bundle?)


    protected fun getTextColor(colorRes: Int): Int {
        return resources.getColor(colorRes)
    }

    protected fun getDimensionValue(dimension: Int): Int {
        return resources.getDimensionPixelSize(dimension)
    }

    protected fun getDimensionValue2(dimension: Int): Float {
        return resources.getDimension(dimension)
    }

    protected fun getTextContent(contentId: Int): String {
        return resources.getString(contentId)
    }
}